﻿using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.IO;

namespace AutoUnitTestEngine.Helpers
{
    public class ExcelHelper
    {
        private readonly string m_StrConn;

        public ExcelHelper(string filePath)
        {
            m_StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source="
                        + filePath + ";" + @"Extended Properties=""Excel 8.0;HDR=Yes;""";
        }

        public DataTable GetDataTableFromExcel(string sheetName)
        {
            var dt = new DataTable(sheetName);
            var conn = new OleDbConnection(m_StrConn);
            var adapter = new OleDbDataAdapter("select * from [" + sheetName + "$]", conn);
            adapter.Fill(dt);
            return dt;
        }

        public void OutputExcel(string filePath,List<DataTable> dts)
        {
            if (File.Exists(filePath)) File.Delete(filePath);
            var excelGenerator = new ExcelFileGenerator(filePath);
            excelGenerator.ExportDataToExcelFile(dts);
        }
    }
}